<#include "../layout/base.ftl">
<#macro header>
<script type="text/javascript">
var companyList = [
    <#if companyList??&&companyList?size&gt;0>
        {text: "", value: "0"},
        <#list companyList as company>
            {text: "${company[2]!""}", value: "${company[0]!""}"}<#if company_has_next>,</#if>
        </#list>
    </#if>
];

$(function () {
    $("#rp_search_panel").omPanel({
        title: "高级搜索"
    });

    $("#rp_type").omCombo({
        width: 100,
        dataSource: [
            {text: '收款', value: '1'},
            {text: '付款', value: '2'}
        ],
        value: '1',
        editable: false
    });

    var start = new Date();
    start.setFullYear(${startTime?string("yyyy")!""}, ${startTime?string("M")!""}-1,${startTime?string("dd")!""});
    var end = new Date();
    end.setFullYear(${endTime?string("yyyy")!""}, ${endTime?string("M")!""}-1,${endTime?string("dd")!""});

    $("#rp_start_time").omCalendar({
        width: 100,
        maxDate: end,
        editable: false,
        onSelect: function (date, event) {
            $("#rp_end_time").omCalendar({
                width: 100,
                minDate: date,
                editable: false
            });
        }
    });

    $("#rp_end_time").omCalendar({
        width: 100,
        minDate: start,
        editable: false,
        onSelect: function (date, event) {
            $("#rp_start_time").omCalendar({
                width: 100,
                maxDate: date,
                editable: false
            });
        }
    });

    $('#rp_customer_name').omSuggestion({
        width: 100,
        listWidth: 300,
        cacheSize: 0,
        method: 'POST',
        dataSource: '${base}/basic/find_customer_json',
        clientFormatter: function (data, index) {
            return data.name;
        },
        onSelect: function (rowData, text, index, event) {
            $("#rp_customer_id").val(rowData.id);
        }
    });

    $("#rp_customer_name").keydown(function (event) {
        var curKey = event.which;
        if (curKey == 8 || curKey == 46) {
            $("#rp_customer_id").val("");
            $("#rp_customer_name").val("");
        }
    });

    $("#rp_company_name").omCombo({
        width: 100,
        dataSource: companyList,
        listAutoWidth: true,
        filterStrategy: 'anywhere',
        editable: false,
        value: '0'
    });

    $("#rp_status").omCombo({
        width: 100,
        dataSource: [
            {text: '未导入', value: '1'},
            {text: '导入久其成功', value: '7'},
            {text: '从久其删除成功', value: '9'},
            {text: '导入久其失败', value: '6'},
            {text: '从久其删除失败', value: '8'},
            {text: '导入U8成功', value: '3'},
            {text: '从U8删除成功', value: '5'},
            {text: '导入U8失败', value: '2'},
            {text: '从U8删除失败', value: '4'}
        ],
        value: '1',
        editable: false
    });

    $('span#rp_button_search').omButton({
        width: 70,
        icons: {left: '${base}/css/default/images/advanced/search.png'},
        onClick: function (event) {
            $('#rp_grid').omGrid({
                extraData: {type: $("#rp_type").omCombo('value'),
                    startTime: $("#rp_start_time").val(),
                    endTime: $("#rp_end_time").val(),
                    companyId: $("#rp_company_name").omCombo('value'),
                    customerId: $("#rp_customer_id").val(),
                    billNumber: $("#rp_bill_number").val(),
                    voucherId: $("#rp_voucher_id").val(),
                    operator: $("#rp_operator").val(),
                    status: $("#rp_status").val()}
            });
        }
    });

    var grid = $('#rp_grid').omGrid({
        method: 'POST',
        dataSource: "${base}/receipt_payment/grid_json",
        extraData: {type: $("#rp_type").omCombo('value'),
            startTime: $("#rp_start_time").val(),
            endTime: $("#rp_end_time").val(),
            companyId: $("#rp_company_name").omCombo('value'),
            customerId: $("#rp_customer_id").val(),
            billNumber: $("#rp_bill_number").val(),
            voucherId: $("#rp_voucher_id").val(),
            operator: $("#rp_operator").val(),
            status: $("#rp_status").omCombo('value')},
        height: 380,
        width: 'fit',
        autoFit: true,
        limit: 10,
        colModel: [
            {header: '单据号码', name: 'no', align: 'left'},
            {header: '收款日期', name: 'time', align: 'left'},
            {header: '收款类型', name: 'typeName', align: 'left'},
            {header: '客户名称', name: 'custom', align: 'left'},
            {header: '收款金额', name: 'sum', align: 'left'},
            {header: '票据号码', name: 'billNumber', align: 'left'},
            {header: '入帐银行', name: 'bank', align: 'left'},
            {header: '类型', name: 'stypeName', align: 'left'},
            {header: '审核人', name: 'operator', align: 'left'},
            {header: '公司名称', name: 'company', align: 'left'},
            {header: '久其凭证号', name: 'voucherId', align: 'left'},
            {header: '操作', name: 'operation', align: 'center', renderer: function (colValue, rowData, rowIndex) {
                if (colValue == "add")
                    return '<button onClick="rpShowDialog(' + rowData.id + ')">导入</button>';
                if (colValue == "delete")
                    return '<button onClick="rpRevokeVoucher(' + rowData.id + ')">删除凭证</button>';
                if (colValue == "reset")
                    return '<button onClick="rpRestVoucher(' + rowData.id + ')">重置</button>';
                return '';
            }}
        ]
    });

    $(window).scroll(function () {
        $("#rp_grid").omGrid('resize');
    });

    $("#rp_dialog").omDialog({
        title: "导入凭证",
        autoOpen: false,
        resizable: true,
        modal: true,
        width: 800,
        buttons: [
            {
                text: "确定",
                click: function () {
                    var rows = $("#rp_items tr");
                    var items = "[";
                    for (var i = 0; i < rows.length; i++) {
                        var inputs = $(rows[i]).find(":input");
                        items += "{";
                        for (var j = 0; j < inputs.length; j++) {
                            items += "\"" + $(inputs[j]).attr("name") + "\":\"" + $(inputs[j]).val() + "\",";
                        }
                        items = items.substring(0, items.length - 1) + "},";
                    }
                    items = items.substring(0, items.length - 1) + "]";
                    $.ajax({
                        type: "POST",
                        url: '${base}/receipt_payment/generate_voucher_json',
                        data: {'id': $("#rp_id").val(), 'attachment': $("#rp_attachment").val(),
                            importType: $("#rp_importType").omCombo("value"), voucherNum: $("#rp_voucherNumber").val(),
                            voucherItems: items},
                        dataType: 'json',
                        cache: false,
                        beforeSend: function () {
                            $("#rp_dialog").omDialog("close");
                            $.blockUI({ message: '<h2>请稍后...</h2>' });
                        },
                        success: function (data) {
                            $.unblockUI();
                            $('span#rp_button_search').omButton("click");
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }
                    });
                }
            },
            {
                text: "取消",
                click: function () {
                    $("#rp_dialog").omDialog("close");
                }
            }
        ]
    });
});

var rp_item_table_row = '<tr><td width="20%" style="padding: 0px"><input type="text" name="digest" value=":digest"/></td>' +
        '<td width="20%" align="left">:subjectname</td>' +
        '<td width="10%" align="right">:debit</td>' +
        '<td width="10%" align="right">:credit</td>' +
        '<td width="20%">:departmentname</td>' +
        '<td width="20%"></td></tr>';

function rpShowDialog(id) {
    var data = {"id": id};
    $("#rp_id").val(id);
    $.post("${base}/receipt_payment/voucher_json", data, function (data, textStatus, jqXHR) {
        if (data.responseflag == "0") {
            $.omMessageBox.alert({
                type: "error",
                title: '提示',
                content: data.responsemessage
            });
            return;
        }
        $("#rp_vchrtypecode").html(data.voucher.master.vchrtypecode);
        $("#rp_createuser").html(data.voucher.master.createuser);
        $("#rp_createdate").val(data.voucher.master.systemdate);
        $("#rp_createdate").omCalendar({
            editable: false
        });
        $("#rp_items").html("");
        if (data.voucher.items.length != 0) {
            var items_table = "";
            for (var i = 0; i < data.voucher.items.length; i++) {
                var item = data.voucher.items[i];
                var temp = rp_item_table_row.replace(":digest", item.digest);
                temp = temp.replace(":subjectname", item.subjectname);
                temp = temp.replace(":debit", item.debit);
                temp = temp.replace(":credit", item.credit);
                temp = temp.replace(":departmentname", item.departmentname);
                items_table += temp;
            }
            $("#rp_items").html(items_table);
        }
        $("#rp_importType").omCombo({
            width: 138,
            dataSource: [
                {text: '生成凭证号', value: '1'},
                {text: '指定凭证号', value: '2'}
            ],
            value: '1',
            editable: false
        });
        $("#rp_attachment").val("");
        $("#rp_dialog").omDialog('open');
    }, "json");
}

function rpRevokeVoucher(id) {
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认撤销该凭证？',
        onClose: function (v) {
            if (v) {
                var data = {"id": id};
                $.post("${base}/receipt_payment/revoke_voucher_json", data,
                        function (data, textStatus, jqXHR) {
                            if (data.responseflag == "1") {
                                $('span#rp_button_search').omButton("click");
                            }
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }, "json");
            }
        }
    });
}

function rpRestVoucher(id) {
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认重置该凭证？',
        onClose: function (v) {
            if (v) {
                var data = {"id": id};
                $.post("${base}/receipt_payment/rest_voucher_json", data,
                        function (data, textStatus, jqXHR) {
                            if (data.responseflag == "1") {
                                $('span#rp_button_search').omButton("click");
                            }
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }, "json");
            }
        }
    });
}
</script>
</#macro>
<#macro body>
<div id="rp_search_panel" class="search_panel">
    <div>
        <span class="label">类&nbsp;&nbsp;&nbsp;型：</span>
        <input id="rp_type"/>
        <span class="label">时&nbsp;&nbsp;&nbsp;间：</span>
        <input id="rp_start_time" style="width:80px" value="${startTime?string("yyyy-MM-dd")!""}"/>
        <span class="label">&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <input id="rp_end_time" style="width:80px" value="${endTime?string("yyyy-MM-dd")!""}"/>
        <span class="label">客&nbsp;&nbsp;&nbsp;户：</span>
        <input id="rp_customer_name" class="input-text"/>
        <input id="rp_customer_id" type="hidden"/>
        <span class="label">票据号：</span>
        <input id="rp_bill_number" type="text" class="input-text"/>
        <br/><br/>
        <span class="label">公&nbsp;&nbsp;&nbsp;司：</span>
        <input id="rp_company_name" type="text"/>
        <span class="label">凭证号：</span>
        <input id="rp_voucher_id" type="text" class="input-text"/>
        <span class="label">操作人：</span>
        <input id="rp_operator" type="text" class="input-text"/>
        <span class="label">状&nbsp;&nbsp;&nbsp;态：</span>
        <input id="rp_status" type="text"/>&nbsp;&nbsp;
        <span id="rp_button_search">搜索</span>
    </div>
</div>
<br/>
<table id="rp_grid"></table>

<div id="rp_dialog">
    <form action='' method='post'>
        <input id="rp_id" type="hidden"/>
        <table width="100%">
            <tr>
                <td width="70px">字号</td>
                <td><span id="rp_vchrtypecode" style="font-size: 12px;font-weight: bold"></span></td>
                <td width="70px">制单人</td>
                <td id="rp_createuser">
                </td>
                <td width="70px">制单日期</td>
                <td>
                    <input id="rp_createdate" type="text" style="width: 120px"/>
                </td>
                <td width="70px">附单据数</td>
                <td>
                    <input id="rp_attachment" type="text" style="width: 120px"/>
                </td>
            </tr>
        </table>
        <br/>
        <table width="100%" class="form outtable">
            <thead>
            <tr>
                <th>摘要</th>
                <th>科目</th>
                <th>借方</th>
                <th>贷方</th>
                <th>部门</th>
                <th>项目</th>
            </tr>
            </thead>
            <tbody id="rp_items">
            </tbody>
        </table>
        <table width="100%">
            <tr>
                <td width="70px">导入方式</td>
                <td><input id="rp_importType"/></td>
                <td width="70px">凭证号</td>
                <td><input id="rp_voucherNumber" type="text" style="width: 118px"/></td>
            </tr>
        </table>
    </form>
</div>
</#macro>